//
//  AllocateFloorCell.m
//  QRMaster
//
//  Created by myApple on 15/6/23.
//  Copyright (c) 2015年 myApple. All rights reserved.
//

#import "AllocateFloorCell.h"


@interface AllocateFloorCell()

@property (nonatomic,strong) UIImageView * arrowImgViewDown;//展开按钮
@property (nonatomic,strong) UIImageView * arrowImgViewLeft;
@property (nonatomic,strong) UILabel  * nameLable;//名字
@property (nonatomic,strong) UILabel * selectNumLabel;//选中和总共数据的显示
@property (nonatomic,strong) ItemModel * itemInfo;
@property (nonatomic,strong) UIView * lineView;

@end

@implementation AllocateFloorCell

#pragma mark -------init
+(instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString * cellId = @"GroupTableViewCell";
    AllocateFloorCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[AllocateFloorCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    return cell;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        [self addAllSubView];
    }
    return self;
}

-(void)addAllSubView
{
    [self addSubview:self.arrowImgViewDown];
    [self addSubview:self.arrowImgViewLeft];
    [self addSubview:self.nameLable];
    [self addSubview:self.selectNumLabel];
    [self addSubview:self.lineView];
}


#pragma mark ------initData
-(void)setData:(ItemModel *)itemInfo
{
//    self.itemInfo = itemInfo;
//    self.nameLable.text = self.itemInfo.itemName;
//    self.selectNumLabel.text = [NSString stringWithFormat:@"%d/%d",self.itemInfo.itemSelect,self.itemInfo.itemSum];
//     _arrowImgViewLeft.image = [UIImage imageNamed:@"treeIconLeft"];
////     _arrowImgViewDown.image = [UIImage imageNamed:@"treeIconDown"];
//    if (self.itemInfo.isExpanded) {
//         _arrowImgViewLeft.frame = CGRectMake(16, (self.frame.size.height - 7)/2.0, 11, 8);
//        if (self.itemInfo.isClick) {
//            //当前是被点击且还是展开的
////            _arrowImgViewDown.hidden = YES;
//            [UIView animateWithDuration:0.2 animations:^{
//                CGAffineTransform transform =CGAffineTransformMakeRotation(M_PI_2);
//                [_arrowImgViewLeft setTransform:transform];
//            } completion:^(BOOL finished) {
//                _arrowImgViewLeft.hidden = NO;
//                self.lineView.hidden = YES;
//            }];
//        }
//        else
//        {
//            //当前不是点击的但是是展开的
//            
//        }
//        
//       
//    }
//    else
//    {
//        if (self.itemInfo.isClick) {
//            _arrowImgViewLeft.frame = CGRectMake(16, (self.frame.size.height - 7)/2.0, 8, 11);
//            //当前是点击的但是不是展开的
////            _arrowImgViewLeft.hidden = YES;
//            [UIView animateWithDuration:0.2 animations:^{
//                CGAffineTransform transform =CGAffineTransformMakeRotation(M_PI_2 * 4);
//                [_arrowImgViewLeft setTransform:transform];
//            } completion:^(BOOL finished) {
//                self.lineView.hidden = NO;
////                _arrowImgViewDown.hidden = NO;
//            }];
//        }
//        else
//        {
//            //当前不是点击的且不是展开的
//            self.lineView.hidden = NO;
////            _arrowImgViewDown.hidden = YES;
//            _arrowImgViewLeft.hidden =  NO;
//        }
//
//    }
    
//    ===================
    
    self.itemInfo = itemInfo;
    self.nameLable.text = self.itemInfo.itemName;
    self.selectNumLabel.text = [NSString stringWithFormat:@"%ld/%ld",(long)self.itemInfo.itemSelect,(long)self.itemInfo.itemSum];
    _arrowImgViewLeft.image = [UIImage imageNamed:@"treeIconLeft"];
    if (self.itemInfo.isExpanded) {
        _arrowImgViewLeft.frame = CGRectMake(16, (self.frame.size.height - 7)/2.0, 11, 8);
        if (self.itemInfo.isClick) {
            //当前是被点击且还是展开的
            //            _arrowImgViewDown.hidden = YES;
            [UIView animateWithDuration:0.2 animations:^{
                CGAffineTransform transform =CGAffineTransformMakeRotation(M_PI_2);
                [_arrowImgViewLeft setTransform:transform];
            } completion:^(BOOL finished) {
                _arrowImgViewLeft.hidden = NO;
                self.lineView.hidden = YES;
            }];
        }
        else
        {
            //当前不是点击的但是是展开的
        }
        
    }
    else
    {
        if (self.itemInfo.isClick) {
            _arrowImgViewLeft.frame = CGRectMake(16, (self.frame.size.height - 7)/2.0, 8, 11);
            //当前是点击的但是不是展开的
            //            _arrowImgViewLeft.hidden = YES;
            [UIView animateWithDuration:0.2 animations:^{
                CGAffineTransform transform =CGAffineTransformMakeRotation(M_PI_2 * 4);
                [_arrowImgViewLeft setTransform:transform];
            } completion:^(BOOL finished) {
                self.lineView.hidden = NO;
                //                _arrowImgViewDown.hidden = NO;
            }];
        }
        else
        {
            //当前不是点击的且不是展开的
            self.lineView.hidden = NO;
            //            _arrowImgViewDown.hidden = YES;
            _arrowImgViewLeft.hidden =  NO;
        }
        
    }

}

#pragma mark -----------layOutView
-(void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat xOffSet = 15;
    if (self.itemInfo) {
        xOffSet = 16;
    }
    
    // 图标
    if (self.itemInfo.nodeLevel==1) {
        xOffSet = 16;
    }else{
        xOffSet = 22;
    }
    if (self.itemInfo.isExpanded) {
        _arrowImgViewLeft.frame = CGRectMake(xOffSet, (self.frame.size.height - 7)/2.0, 11, 8);
    }else
    {
        _arrowImgViewLeft.frame = CGRectMake(xOffSet, (self.frame.size.height - 7)/2.0, 8, 11);
    }

    
    // 名字
    if (self.itemInfo.nodeLevel==1) {
        xOffSet = 45;
        _nameLable.frame = CGRectMake(xOffSet, (self.frame.size.height - 20)/2.0, self.frame.size.width - xOffSet, 20);
    }else{
        xOffSet = 51;
        _nameLable.frame = CGRectMake(xOffSet, (self.frame.size.height - 20)/2.0, self.frame.size.width - xOffSet, 20);
    }
    
    _selectNumLabel.frame = CGRectMake(UI_SCREEN_WIDTH-16-40, 14, 40, 16);
    
    // 线
    if (self.itemInfo.nodeLevel == 2) {
        self.lineView.frame = CGRectMake(16, self.frame.size.height - 0.5, self.frame.size.width, 0.5);
    }
    else
    {
        self.lineView.frame = CGRectMake(0, self.frame.size.height - 0.5, self.frame.size.width, 0.5);
    }

}

#pragma mark ---------lazy
-(UILabel *)nameLable
{
    if (!_nameLable) {
        _nameLable  = [[UILabel alloc]init];
        _nameLable.font = [UIFont systemFontOfSize:16.0];
    }
    return _nameLable;
}

-(UIImageView *)arrowImgViewLeft
{
    if (!_arrowImgViewLeft) {
        _arrowImgViewLeft = [[UIImageView alloc]init];
    }
    return _arrowImgViewLeft;
}

-(UIImageView *)arrowImgViewDown
{
    if (!_arrowImgViewDown) {
        _arrowImgViewDown = [[UIImageView alloc]init];
        _arrowImgViewLeft.image = [UIImage imageNamed:@"treeIconLeft"];

    }
    return _arrowImgViewDown;
}

-(UILabel *)selectNumLabel
{
    if (!_selectNumLabel) {
        _selectNumLabel = [[UILabel alloc]init];
        _selectNumLabel.font = [UIFont systemFontOfSize:15.0];
        _selectNumLabel.textAlignment = NSTextAlignmentRight;
    }
    return _selectNumLabel;
}

-(UIView *)lineView
{
    if (!_lineView) {
        _lineView = [[UILabel alloc]init];
        _lineView.backgroundColor = UIColorFromRGB(0xc1c1c1);
    }
    return _lineView;
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
